package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.exception.BaseException;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 添加套餐
     * @param setmealDTO
     * @return
     */
    @Override
    public Result addSetmeal(SetmealDTO setmealDTO) {
        /*新增套餐,可以理解为新增菜品,选一个套餐分类
        * 往套餐表里面加东西,我们去造一个套餐实体去接受属性然后添加
        * */
        Setmeal setmeal=new Setmeal();
        //数据的拷贝
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.addSetmeal(setmeal);
        //我们得到了新加入套餐的id,我们去获得当前套餐所携带的菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //为每一个套餐里面的菜品赋上套餐id
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        //然后加入到套餐菜品表中
        setmealDishMapper.addSetmealDish(setmealDishes);
        return Result.success();


    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        int pageNum = setmealPageQueryDTO.getPage();
        int pageSize = setmealPageQueryDTO.getPageSize();

        PageHelper.startPage(pageNum, pageSize);
        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());

    }

    /**
     *
     * @param ids
     */

    @Override
    public Result deleteByIds(Long[] ids) {
        /*首先查询套餐是否是起售状态*/
        Integer i = setmealMapper.statusById(ids);
        if (i>0){
            throw new   BaseException("当前套餐在起售不能被删除");
        }
        /*删除套餐*/
        setmealMapper.deleteByIds(ids);
        /*删除根据ids删除套餐菜品表里面的东西*/
        setmealDishMapper.deleteByIds(ids);
        return Result.success();

    }

    /**
     * 根据id查询
     * @param setmealId
     */

    @Override
    public Result findById(Long setmealId) {
        SetmealVO byId = setmealMapper.findById(setmealId);

        return Result.success(byId);

    }

    /**
     * 修改
     * @param setmealDTO
     * @return
     */

    @Override
    public Result updateSetmeal(SetmealDTO setmealDTO) {
        //造套餐实体
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);

        /*修改的时候先把套餐菜品的全部删除掉*/
        Long id = setmealDTO.getId();
        Long [] ids={id};

        setmealDishMapper.deleteByIds(ids);
        //然后再重新去添加不为空才去添加
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes!=null){
            for (SetmealDish setmealDish : setmealDishes) {
                //为每一个菜品添加套餐对象
                setmealDish.setSetmealId(setmeal.getId());
            }
            //添加到套餐菜品里面
            setmealDishMapper.addSetmealDish(setmealDishes);
        }

        return Result.success();
    }

    /**
     * 起售停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Long status, Long id) {
        setmealMapper.startOrStop(status,id);

    }
    //微信小程序代码导入
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list =(List<Setmeal>) redisTemplate.opsForValue().get("dish:categoryId:" + setmeal.getCategoryId());
        if(list!=null&&list.size()>0){
            return list;
        }
        list = setmealMapper.list(setmeal);
        redisTemplate.opsForValue().set("dish:categoryId:" + setmeal.getCategoryId(),list);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
